var trans = {'0':'支出','1':'收入','2':'审核成功'};


$(document).ready(function() {
    // fuelux datagrid
/*    var DataGridDataSource = function (options) {
        this._formatter = options.formatter;
        this._columns = options.columns;
        this._delay = options.delay;
    };

    DataGridDataSource.prototype = {

        columns: function () {
            return this._columns;
        },

        data: function (options, callback) {
            var url = 'http://120.24.42.44:8888/boss/admin/admin/detailAll';
            var self = this;

            setTimeout(function () {

                var data = $.extend(true, [], self._data);
                var adminAccount = parseInt(localStorage.id);
                $.ajax(url, {
                    data: {aid: adminAccount},
                    dataType: 'json',
                    async: false,
                    type: 'GET'
                }).done(function (response) {

                    data = response;

                    // SEARCHING 查找
                    if (options.search) {
                        data = _.filter(data, function (item) {
                            var match = false;

                            _.each(item, function (prop) {
                                if (_.isString(prop) || _.isFinite(prop)) {
                                    if (prop.toString().toLowerCase().indexOf(options.search.toLowerCase()) !== -1) match = true;
                                }
                            });

                            return match;
                        });
                    }

                    // FILTERING 过滤
                    if (options.filter) {
                        data = _.filter(data, function (item) {
                            switch(options.filter.value) {
                                case 'lt5m':
                                    if(item.population < 5000000) return true;
                                    break;
                                case 'gte5m':
                                    if(item.population >= 5000000) return true;
                                    break;
                                default:
                                    return true;
                                    break;
                            }
                        });
                    }

                    var count = data.length;

                    // SORTING 排序
                    if (options.sortProperty) {
                        data = _.sortBy(data, options.sortProperty);
                        if (options.sortDirection === 'desc') data.reverse();
                    }

                    // PAGING
                    var startIndex = options.pageIndex * options.pageSize;
                    var endIndex = startIndex + options.pageSize;
                    var end = (endIndex > count) ? count : endIndex;
                    var pages = Math.ceil(count / options.pageSize);
                    var page = options.pageIndex + 1;
                    var start = startIndex + 1;

                    data = data.slice(startIndex, endIndex);

                    if (self._formatter) self._formatter(data);

                    callback({ data: data, start: start, end: end, count: count, pages: pages, page: page });
                }).fail(function(e){

                });
            }, self._delay);
        }
    };

    $('#MyStretchGrid').each(function() {
        $(this).datagrid({
            dataSource: new DataGridDataSource({
                // Column definitions for Datagrid
                columns: [
                    {
                        property: 'hcreatetime',
                        label: '日期',
                        sortable: true
                    },
                    {
                        property: 'htype',
                        label: '操作',
                        sortable: true
                    },
                    {
                        property: 'bid',
                        label: '账号',
                        sortable: true
                    },
                    {
                        property: 'hbalance',
                        label: '金额'
                        //sortable: true
                    }
                ],
                // Create IMG tag for each returned image
                formatter: function (items) {

                    $.each(items, function (index, item) {

                        if(item.htype === "0"){
                            item.htype = "支出"
                        }
                        if(item.htype === "1"){
                            item.htype = "收入"
                        }
                    });
                }
            })
        });
    });*/
    var adminAccount = parseInt(localStorage.id);
    $.ajax({
        type: "get",//请求方式,post\get只要在这里改，其他地方不要改
         url : 'http://120.24.42.44:8888/boss/admin/admin/detailAll',//访问的地址
        data: {aid :adminAccount},// 要提交的数据,
        dataType:'json',//传入数据的格式，可以统一用json
        success: function (data) {

            $('#dt-ad').DataTable({
                data: data,
                columns: [
                    { title: "日期" , data:'hcreatetime'},
                    { title: "操作" ,data:'htype', render:function (data,type,row) {
                        return trans[data];}},
                    { title: "账号" ,data:'bid'},
                    { title: "金额",data:'hbalance' }
                ]
            });

        },
        error: function (error) {

        }
    });

    //$("#dt-ad_filter").setAttribute("display", "none");
    var jqxhr = $.get('http://120.24.42.44:8888/boss/admin/admin/netincome',{
                aid: 1
    } ).success(function(a){

        $('#money').text(a);
    })
});

function search(){




    endTime =  $("#endtime").val();
    startTime= $("#starttime").val();
    aid = parseInt(localStorage.id);

    //重新获取数据并展示
    $.ajax({
        type: "get",//请求方式,post\get只要在这里改，其他地方不要改
        url : 'http://120.24.42.44:8888/boss/admin/admin/detailByTime',//访问的地址
        data: {aid: aid,
        startTime: startTime,
        endTime: endTime},// 要提交的数据,
        dataType:'json',//传入数据的格式，可以统一用json
        success: function (data) {

            var dttable=$('#dt-ad').dataTable();
            dttable.fnClearTable(); //清空一下table
            dttable.fnDestroy(); //还原初始化了的datatable

            $('#dt-ad').DataTable({
                data: data,
                columns: [
                    { title: "日期" , data:'hcreatetime'},
                    { title: "操作" ,data:'htype', render:function (data,type,row) {
                return trans[data];}},
                    { title: "账号" ,data:'bid'},
                    { title: "金额",data:'hbalance' }
                ]
            });
        },
        error: function (error) {

        }
    });

}